var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const dateTimePicker = require('../../../utils/dateTimePicker.js');
var app = getApp();

Page({
  data: {
    username: '',
    realGender: '',
    birthday: '2018-10-01',
    lastLoginTime: null,
    mobile: '',
    deptName: '',
    nickname:'',
    dateTimeArray: null,
    startYear: 2020,
    endYear: 2050,
  },
  bindPhoneNumber: function(e) {
    if (e.detail.errMsg !== "getPhoneNumber:ok") {
      // 拒绝授权
      return;
    }

    if (!this.data.hasLogin) {
      wx.showToast({
        title: '绑定失败：请先登录',
        icon: 'none',
        duration: 2000
      });
      return;
    }

    util.request(api.AuthBindPhone, {
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData
    }, 'POST').then(function(res) {
      if (res.errno === 0) {
        wx.showToast({
          title: '绑定手机号码成功',
          icon: 'success',
          duration: 2000
        });
      }
    });
  },

  bindUsernameInput: function(e) {
    this.setData({
      username: e.detail.value
    });
  },
changeDate(e){
  this.setData({birthday:e.detail.value});
},

  onLoad: function(options) {
   // 页面加载获取用户信息
   this.getUserInfo();

  },


  updateUserInfo: function() {
    var that = this;
    wx.request({
      url: api.updateUser,
      data: {
				username: this.data.username,
        birthday:  this.data.birthday,
				mobile:  this.data.mobile,
				nickname:  this.data.nickname,
				id:wx.getStorageSync('userId')
      },
      method: 'POST',
      header: {
        'content-type': 'application/json'
      },
      success: function(res) {
        if (res.data.errno == 0) {
		       wx.showToast({
		        title: '保存成功',
		        icon: 'success',
		        duration: 2000
		      });
        } else {
          wx.showModal({
            title: '错误信息',
            content: res.data.errmsg,
            showCancel: false
          });
        }
      }
    });
  },

  getUserInfo: function() {
    let that = this;
    console.log("99999999")
    util.request(api.getUserInfo, {
      userId:wx.getStorageSync('userId')
    }).then(function(res) {
      if (res.errno === 0) {
        console.log("========"+res.data.birthday)
        try{
        that.setData({
          username: res.data.username,
          realGender:  res.data.realGender,
          birthday:  res.data.birthday,
          lastLoginTime:  res.data.lastLoginTime,
          mobile:  res.data.mobile,
          deptName:  res.data.deptName,
          nickname:  res.data.nickname
        });
        }catch (err) {
          console.log(err.stack);
        }

      }
    });
  },

  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {
    // 页面显示
    this.getUserInfo();
  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  }
})